/*
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.
*/

class Solution {
public:
    bool isPalindrome(string s) {
        for (int start = 0, end = s.length()-1; start < end; start++, end--) {
            while (!isalnum(s[start])) {
                start++;
                if (start == end) return true;
            }
            while (!isalnum(s[end])) {
                end--;
                if (end == start) return true;
            }
            if (tolower(s[start]) != tolower(s[end])) return false;
        }
        return true;
    }
};

#if 0
class Solution {
public:
    bool isPalindrome(string s) {
        auto start = s.begin();
        auto end = prev(s.end());
        transform(s.begin(), s.end(), s.begin(), ::tolower);
        while (start < end) {
            while (!isalnum(*start)) {
                start ++;  // first alphabetic
                if (start == end) return true;
            }
            while (!isalnum(*end)) {
                end --;    // first alphabetic
                if (end == start) return true;
            }
            if (*start != *end) return false;
            // move to next char
            start ++; end--;
        }
        return true;
    }
};
#endif

